<?php
class ModelCatalogNews extends Model {
    public function addNews($data) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "news SET sort_order = '" . (int)$this->request->post['sort_order'] . "', status = '" . (int)$data['status'] . "', image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "'");

        $new_id = $this->db->getLastId();

        foreach ($data['new_description'] as $language_id => $value) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "news_description SET news_id = '" . (int)$new_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', content = '" . $this->db->escape($value['content']) . "'");
        }

        if ($data['keyword']) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'news_id=" . (int)$new_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
        }

        $this->cache->delete('news');
    }

    public function editNews($new_id, $data) {
        $this->db->query("UPDATE " . DB_PREFIX . "news SET sort_order = '" . (int)$data['sort_order'] . "', image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "', status = '" . (int)$data['status'] . "' WHERE news_id = '" . (int)$new_id . "'");

        $this->db->query("DELETE FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int)$new_id . "'");

        foreach ($data['new_description'] as $language_id => $value) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "news_description SET news_id = '" . (int)$new_id . "', language_id = '" . (int)$language_id . "', title = '" . $this->db->escape($value['title']) . "', content = '" . $this->db->escape($value['content']) . "'");
        }

        $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'news_id=" . (int)$new_id. "'");

        if ($data['keyword']) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'news_id=" . (int)$new_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
        }

        $this->cache->delete('news');
    }

    public function activeNews($new_id) {

            $this->db->query("UPDATE " . DB_PREFIX . "news SET status = '" . 1 . "' WHERE news_id = '" . (int)$new_id . "'");


        $this->cache->delete('news');
    }

    public function unactiveNews($new_id) {

        $this->db->query("UPDATE " . DB_PREFIX . "news SET status = '" . 0 . "' WHERE news_id = '" . (int)$new_id . "'");


        $this->cache->delete('news');
    }

    public function deleteNews($new_id) {
        $this->db->query("DELETE FROM " . DB_PREFIX . "news WHERE news_id = '" . (int)$new_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int)$new_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'news_id=" . (int)$new_id . "'");

        $this->cache->delete('news');
    }

    public function getNews($new_id) {
        $query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'news_id=" . (int)$new_id . "') AS keyword FROM " . DB_PREFIX . "news WHERE news_id = '" . (int)$new_id . "'");

        return $query->row;
    }

    public function getNewes($data = array()) {
        if ($data) {
            $sql = "SELECT * FROM " . DB_PREFIX . "news i LEFT JOIN " . DB_PREFIX . "news_description id ON (i.news_id = id.news_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "'";

            $sort_data = array(
                'id.title',
                'i.sort_order'
            );

            if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
                $sql .= " ORDER BY " . $data['sort'];
            } else {
                $sql .= " ORDER BY id.title";
            }

            if (isset($data['order']) && ($data['order'] == 'DESC')) {
                $sql .= " DESC";
            } else {
                $sql .= " ASC";
            }

            if (isset($data['start']) || isset($data['limit'])) {
                if ($data['start'] < 0) {
                    $data['start'] = 0;
                }

                if ($data['limit'] < 1) {
                    $data['limit'] = 20;
                }

                $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
            }

            $query = $this->db->query($sql);

            return $query->rows;
        } else {
            $new_data = $this->cache->get('news.' . $this->config->get('config_language_id'));

            if (!$new_data) {
                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news i LEFT JOIN " . DB_PREFIX . "news_description id ON (i.news_id = id.news_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY id.title");

                $new_data = $query->rows;

                $this->cache->set('news.' . $this->config->get('config_language_id'), $new_data);
            }

            return $new_data;
        }
    }

    public function getNewsDescriptions($new_id) {
        $new_description_data = array();

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news_description WHERE news_id = '" . (int)$new_id . "'");

        foreach ($query->rows as $result) {
            $new_description_data[$result['language_id']] = array(
                'title'       => $result['title'],
                'content' => $result['content']
            );
        }

        return $new_description_data;
    }

    public function getTotalNews() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "news");
        return $query->row['total'];
    }
}
?>